package com.xnfh.field.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xnfh.field.R;


/**
 * Description：土地信息
 */
public class LandInfoView extends FrameLayout {

    private int type = 1;//1：默认，pH值；2：有机质(g/kg)；3：全氮(mg/kg)；4：有效磷(mg/kg)；5：速效钾(mg/kg)；6：土质；

    private TextView tvTitle;
    private TextView tvValue;
    private View tvValueTemp;
    private ImageView ivValue1;//对应level1
    private ImageView ivValue2;//对应level2
    private ImageView ivValue3;//对应level3
    private ImageView ivValue4;//对应level4
    private ImageView ivValue5;//对应level5
    private LinearLayout valueDescLayout;
    private TextView tvValueDesc1;
    private TextView tvValueDesc2;
    private TextView tvValueDesc3;
    private TextView tvValueDesc4;
    private TextView tvValueDesc5;

    public LandInfoView(@NonNull Context context) {
        super(context);
        init();
    }

    public LandInfoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LandInfoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_land_info_layout, this);
        tvTitle = findViewById(R.id.tvTitle);
        tvValue = findViewById(R.id.tvValue1);
        tvValueTemp = findViewById(R.id.tvValueTemp);
        ivValue1 = findViewById(R.id.ivValue1);
        ivValue2 = findViewById(R.id.ivValue2);
        ivValue3 = findViewById(R.id.ivValue3);
        ivValue4 = findViewById(R.id.ivValue4);
        ivValue5 = findViewById(R.id.ivValue5);
        valueDescLayout = findViewById(R.id.valueDescLayout);
        tvValueDesc1 = findViewById(R.id.tvValueDesc1);
        tvValueDesc2 = findViewById(R.id.tvValueDesc2);
        tvValueDesc3 = findViewById(R.id.tvValueDesc3);
        tvValueDesc4 = findViewById(R.id.tvValueDesc4);
        tvValueDesc5 = findViewById(R.id.tvValueDesc5);


        ivValue1.setAlpha(0.5f);
        ivValue2.setAlpha(0.5f);
        ivValue3.setAlpha(0.5f);
        ivValue4.setAlpha(0.5f);
        ivValue5.setAlpha(0.5f);
    }

    public void setType(int type) {
        this.type = type;
//        1：默认，pH值；2：有机质(g/kg)；3：全氮(mg/kg)；4：有效磷(mg/kg)；5：速效钾(mg/kg)；6：土质；
        if (type == 1) {
            tvTitle.setText(getResources().getString(R.string.str_ph));
            valueDescLayout.setVisibility(View.VISIBLE);
            tvValueDesc1.setText(getResources().getString(R.string.str_qiangsuan));
            tvValueDesc2.setText(getResources().getString(R.string.str_suanxing));
            tvValueDesc3.setText(getResources().getString(R.string.str_weisuan));
            tvValueDesc4.setText(getResources().getString(R.string.str_zhongxing));
            tvValueDesc5.setText(getResources().getString(R.string.str_jianxing));
        } else if (type == 2) {
            tvTitle.setText(getResources().getString(R.string.str_youjizhi));
            valueDescLayout.setVisibility(View.VISIBLE);
            tvValueDesc1.setText(getResources().getString(R.string.str_jique));
            tvValueDesc2.setText(getResources().getString(R.string.str_quefa));
            tvValueDesc3.setText(getResources().getString(R.string.str_shizhong));
            tvValueDesc4.setText(getResources().getString(R.string.str_fengfu));
            tvValueDesc5.setText(getResources().getString(R.string.str_jifu));
        } else if (type == 3) {
            tvTitle.setText(getResources().getString(R.string.str_quandan));
            valueDescLayout.setVisibility(View.GONE);
        } else if (type == 4) {
            tvTitle.setText(getResources().getString(R.string.str_youxiaolin));
            valueDescLayout.setVisibility(View.GONE);
        } else if (type == 5) {
            tvTitle.setText(getResources().getString(R.string.str_suxiaojia));
            valueDescLayout.setVisibility(View.GONE);
        } else if (type == 6) {
            tvTitle.setText(getResources().getString(R.string.str_tuzhi));
            valueDescLayout.setVisibility(View.GONE);
            tvValueDesc1.setText(getResources().getString(R.string.str_jique));
            tvValueDesc2.setText(getResources().getString(R.string.str_quefa));
            tvValueDesc3.setText(getResources().getString(R.string.str_shizhong));
            tvValueDesc4.setText(getResources().getString(R.string.str_fengfu));
            tvValueDesc5.setText(getResources().getString(R.string.str_jifu));
        }else if (type == 7) {
            tvTitle.setText(getResources().getString(R.string.str_jianjietan));
            valueDescLayout.setVisibility(View.GONE);
        }
    }

    public void setData(int level, String value) {
        if (level == 1) {
            ivValue1.setAlpha(1f);
        } else {
            ivValue1.setAlpha(0.5f);
        }
        if (level == 2) {
            ivValue2.setAlpha(1f);
        } else {
            ivValue2.setAlpha(0.5f);
        }
        if (level == 3) {
            ivValue3.setAlpha(1f);
        } else {
            ivValue3.setAlpha(0.5f);
        }
        if (level == 4) {
            ivValue4.setAlpha(1f);
        } else {
            ivValue4.setAlpha(0.5f);
        }
        if (level == 5) {
            ivValue5.setAlpha(1f);
        } else {
            ivValue5.setAlpha(0.5f);
        }
        tvValue.setText(value);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tvValueTemp.getLayoutParams();
        params.weight = 5 - level;


    }
}

